package com.jiang.smartcommunity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jiang.smartcommunity.tool.MyAdapter;
import com.jiang.smartcommunity.tool.Tool;
import com.jiang.smartcommunity.ui.activity.ADActivity;
import com.jiang.smartcommunity.ui.activity.NotifyActivity;
import com.jiang.smartcommunity.ui.activity.S1Activity;
import com.jiang.smartcommunity.ui.activity.S2Activity;
import com.jiang.smartcommunity.ui.activity.S3Activity;
import com.jiang.smartcommunity.ui.activity.S4Activity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Banner banner;
    private CardView cardView;
    private RecyclerView rvSer;
    private TextView textView;
    private RecyclerView rvAd;
    private NestedScrollView ns;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, 666);
        }

        getData();

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
    }

    private void getData() {
        ArrayList<String> strings = new ArrayList<>();
        ArrayList<Integer> integers = new ArrayList<>();
        for (NotifyBean notifyBean : Tool.NOTIFY_BEANS) {
            integers.add(notifyBean.res);
            strings.add(notifyBean.t);
        }
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
                .setImages(integers)
                .setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object o, ImageView imageView) {
                        imageView.setImageResource((Integer) o);
                    }
                })
                .setBannerTitles(strings)
                .setOnBannerListener(i -> {
                    Tool.NOTIFY_BEANS_index = i;
                    startActivity(new Intent(requireContext(), NotifyActivity.class));
                }).start();
        List<SerBean> serBeans = Arrays.asList(new SerBean("物业服务", R.drawable.b1), new SerBean("快件管理", R.drawable.b2),
                new SerBean("友邻社交", R.drawable.b3), new SerBean("车辆管理", R.drawable.b4));
        rvSer.setAdapter(new MyAdapter<SerBean>(requireContext(), R.layout.item_bean, serBeans) {
            private ImageView imageView2;
            private TextView textView2;

            @Override
            public void init(View root, int position) {
                imageView2 = (ImageView) root.findViewById(R.id.imageView2);
                textView2 = (TextView) root.findViewById(R.id.textView2);
                SerBean item = getItem(position);
                imageView2.setImageResource(item.res);
                textView2.setText(item.s);
                root.setOnClickListener(v -> {
                    if (position == 0) {
                        startActivity(new Intent(requireContext(), S1Activity.class));
                    } else if (position == 1) {
                        startActivity(new Intent(requireContext(), S2Activity.class));
                    } else if (position == 2) {
                        startActivity(new Intent(requireContext(), S3Activity.class));
                    } else {
                        startActivity(new Intent(requireContext(), S4Activity.class));
                    }
                });
            }
        });
        List<SerBean> ads = Arrays.asList(new SerBean("咖啡机", R.drawable.a1), new SerBean("piupiu", R.drawable.a2),
                new SerBean("雀巢奶粉", R.drawable.a3));
        rvAd.setAdapter(new MyAdapter<SerBean>(requireContext(), R.layout.item_ad, ads) {
            private CardView cv;
            private ImageView imageView3;
            private TextView textView3;

            @Override
            public void init(View root, int position) {
                imageView3 = (ImageView) root.findViewById(R.id.imageView3);
                textView3 = (TextView) root.findViewById(R.id.textView3);
                SerBean item = getItem(position);
                imageView3.setImageResource(item.res);
                textView3.setText(item.s);

                root.setOnClickListener(v -> {
                    Tool.serBean = item;
                    startActivity(new Intent(requireContext(), ADActivity.class));
                });
            }
        });
    }

    private Context requireContext() {
        return this;
    }

    private void initView() {
        banner = (Banner) findViewById(R.id.banner);
        cardView = (CardView) findViewById(R.id.cardView);
        rvSer = (RecyclerView) findViewById(R.id.rv_ser);
        textView = (TextView) findViewById(R.id.textView);
        rvAd = (RecyclerView) findViewById(R.id.rv_ad);
        ns = (NestedScrollView) findViewById(R.id.ns);

        rvSer.setLayoutManager(new GridLayoutManager(this, 4));
        rvAd.setLayoutManager(new LinearLayoutManager(this));
    }
    @Override
    public void onPause() {
        super.onPause();
        System.gc();
    }

    public static class NotifyBean {
        public String t, c;
        public int res;

        public NotifyBean(String t, String c, int res) {
            this.t = t;
            this.c = c;
            this.res = res;
        }
    }

    public static class SerBean {
        public String s;
        public int res;

        public SerBean(String s, int res) {
            this.s = s;
            this.res = res;
        }
    }
}